package com.itheima.controller;

import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import com.itheima.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
public class DeptController {
    @Autowired
    private DeptService deptService;
    /**
     * 查询部门列表
     */
//    @GetMapping("/depts")
//    public List<Dept> list() {
//        List<Dept> list =deptService.list();
//        Result.success(list);
//        return Result;
//    }
    @RequestMapping("/depts")
    public Result list(){
        List<Dept> list = deptService.list();
        return Result.success(list);
    }

//    delete
    @DeleteMapping("/depts")
    public Result delete(Integer id){
        System.out.println("根据id删除部门, id=" + id);
        deptService.delete(id);
        return Result.success();
    }
//  提示:@RequestBody
    @PostMapping("/depts")
    public Result add(@RequestBody Dept dept){
        deptService.add(dept);
        return Result.success();
    }
//  提示：@PathVariable
    @PutMapping("/depts")
    public Result update(@RequestBody Dept dept){
        deptService.update(dept);
        return Result.success();
    }

    @GetMapping("/depts/{id}")
    public Result look(@PathVariable Integer id){
//        Dept dept = new Dept();
        Dept dept = deptService.look(id);
        return Result.success(dept);
    }

}
